//
//  DiscoverClassifyCollectionViewCellSecond.m
//  XYFMApp
//
//  Created by 岁变 on 16/9/7.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "DiscoverClassifyCollectionViewCellSecond.h"
#import "XYMacro.h"

@interface DiscoverClassifyCollectionViewCellSecond () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@end

@implementation DiscoverClassifyCollectionViewCellSecond

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        NSArray *arr = @[@"最新", @"最热"];
        _mySegment = [[UISegmentedControl alloc] initWithItems:arr];
        [self.contentView addSubview:_mySegment];
        _mySegment.clipsToBounds = YES;
        _mySegment.layer.cornerRadius = 15;
        _mySegment.layer.borderWidth = 1;
        _mySegment.layer.borderColor = [UIColor lightGrayColor].CGColor;
        [_mySegment setTintColor:[UIColor lightGrayColor]];
        [_mySegment addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
        _mySegment.selectedSegmentIndex = 0;
        
        NSDictionary *dic = @{NSForegroundColorAttributeName:[UIColor lightGrayColor]};
        [_mySegment setTitleTextAttributes:dic forState:UIControlStateNormal];
        
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.minimumLineSpacing = 0;
        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[BaseCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.bounces = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [self.contentView addSubview:_collectionView];
        [_collectionView registerClass:[DiscoverClassifyCollectionViewCellFifth class] forCellWithReuseIdentifier:@"DiscoverClassifyCollectionViewCellFifth"];
        
    }
    return self;
}

- (void)pageChange:(UISegmentedControl *)segment {
    
    NSInteger index = _mySegment.selectedSegmentIndex;
    [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 2;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    NSInteger index = _collectionView.contentOffset.x / SCREEN_WIDTH;
    _mySegment.selectedSegmentIndex = index;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    DiscoverClassifyCollectionViewCellFifth *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DiscoverClassifyCollectionViewCellFifth" forIndexPath:indexPath];
    cell.contentView.backgroundColor = SCOLOR;
    if (indexPath.item == 0) {
        cell.model = _modelOne;
        
    } else {
        cell.model = _modelTwo;
    }
    cell.cat_id = _cat_id;
    
    return cell;
}

- (void)setModelOne:(ClassifyModelSecond *)modelOne {
    _modelOne = [[ClassifyModelSecond alloc] init];
    _modelOne = modelOne;
    [_collectionView reloadData];
}


- (void)setModelTwo:(ClassifyModelSecond *)modelTwo {
    _modelTwo = [[ClassifyModelSecond alloc] init];
    _modelTwo = modelTwo;
    [_collectionView reloadData];
}



- (void)layoutSubviews {
    
    [super layoutSubviews];
    _mySegment.frame = CGRectMake(120, 10, SCREEN_WIDTH - 240, 30);
    
    _layout.itemSize = CGSizeMake(SCREEN_WIDTH, self.contentView.frame.size.height - 50);
    _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    _collectionView.frame = CGRectMake(0, 50, SCREEN_WIDTH, self.contentView.frame.size.height - 50);
    
}


@end
